<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/10/17
 * Time: 14:24
 */

namespace app\api\controller\user;
use app\api\controller\BasicUserApi;
use think\Db;
use think\Exception;

/**
 * 会员账户控制器
 * Class Account
 * @package app\api\controller\user
 */
class Account extends BasicUserApi
{
    /**
     * 零钱充值
     * @return \think\response\Json
     */
    public function recharge()
    {
        try{
            $post=$this->request->only(['money','code'],'post');
            $validate=new \app\api\validate\Recharge();
            if(!$validate->check($post)){
                return json(['msg'=>$validate->getError()],403);
            }
            $pay_log_id=0;
            Db::transaction(function () use ($post,&$pay_log_id) {
                //删除未支付的记录
                Db::name('recharge')->where('mid',UID)->where('pay_time',0)->delete();
                //插入充值记录
                $r_data['money']=$post['money'];
                $r_data['mid']=UID;
                $r_data['addtime']=time();
                $r_data['pay_time']=0;
                $recharge_id=Db::name('recharge')->insertGetId($r_data);
                //插入支付信息
                $pl_data['order_sn']=create_order_sn(16,'recharge');
                $pl_data['order_id']=$recharge_id;
                $pl_data['money']=$r_data['money'];
                $pl_data['type']=ORDER_TYPE_RECHARGE;
                $pl_data['pay_type']=PAY_TYPE_WEXIN;
                $pl_data['pay_status']=0;
                $pay_log_id=Db::name('pay_log')->insertGetId($pl_data);
            });
            //跳转到支付
            return action('api/other.pay/weixin',['pay_log_id'=>$pay_log_id,'code'=>$post['code']]);
        }catch (Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }
    /**
     * 零钱提现
     * @return \think\response\Json
     */
    public function withdraw()
    {
        try{
            $money=input('money');
            $alipay=input('alipay');
            $name=input('name');
            //查询这个人的可用余额
            $user_money=Db::name('store_member')->where('id',UID)->value('money');
            if($money>$user_money||$money<0){
                return json(['msg'=>'提现金额无效'],500);
            }
            Db::transaction(function() use ( $money ,$alipay ,$name){
                //插入提现记录
                $w_data['money']=$money;
                $w_data['alipay']=$alipay;
                $w_data['name']=$name;
                $w_data['mid']=UID;
                $w_data['addtime']=time();
                $w_data['deal_time']=0;
                $w_data['deal_status']=0;
                Db::name('withdraw')->insert($w_data);
                //扣除用户金额
                Db::name('store_member')->where('id',UID)->setDec('money',$w_data['money']);
            });
            return json(['msg'=>'申请成功'],200);
        }catch (Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }
}